package com.hopes.bizz.risk.transfer.controller;

import com.hopes.bizz.common.core.util.CollectionUtils;
import com.hopes.bizz.risk.transfer.bean.bo.RiskTransferResult;
import com.hopes.bizz.risk.transfer.bean.dto.RiskDataDto;
import com.hopes.bizz.risk.transfer.bean.log.dto.RiskErrorLogDTO;
import com.hopes.bizz.risk.transfer.bean.log.dto.RiskLogDTO;
import com.hopes.bizz.risk.transfer.bean.log.vo.RiskErrorLogVo;
import com.hopes.bizz.risk.transfer.bean.log.vo.RiskLogResult;
import com.hopes.bizz.risk.transfer.bean.log.vo.RiskLogVo;
import com.hopes.bizz.risk.transfer.constant.RiskErrorType;
import com.hopes.bizz.risk.transfer.constant.RiskRequestType;
import com.hopes.bizz.risk.transfer.constant.RiskStatusCode;
import com.hopes.bizz.risk.transfer.service.RiskReceiveService;
import com.hopes.bizz.risk.transfer.service.RiskTransferLogService;
import com.hopes.boss.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lyb
 * @version 1.0
 * @date 2024/1/31 16:02
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/v2/data/receive")
@Tag(description = "RiskReceive", name = "双预防数据交换")
@SecurityRequirement(name = HttpHeaders.ALLOW)
public class RiskReceiveController {

    private final RiskReceiveService riskReceiveService;

    private final RiskTransferLogService transferLogService;

    @Inner(value = false)
    @PostMapping("receiveUnitList")
    public RiskTransferResult receiveUnitList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_UNIT);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("receiveEventList")
    public RiskTransferResult receiveEventList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_EVENT);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("receiveControlMeasuresList")
    public RiskTransferResult receiveControlMeasuresList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_CONTROL_MEASURES);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("receiveMeasuresTaskList")
    public RiskTransferResult receiveMeasuresTaskList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_MEASURES_TASK);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("receiveMeasuresTaskRecordList")
    public RiskTransferResult receiveMeasuresTaskRecordList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_MEASURES_TASK_RECORD);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("receiveDangerInvestigationList")
    public RiskTransferResult receiveDangerInvestigationList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_DANGER_INVESTIGATION);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("receiveUnitStopList")
    public RiskTransferResult receiveUnitStopList(@RequestBody RiskDataDto riskDataDto) {
        String batchId = riskReceiveService.handlerRiskData(riskDataDto, RiskRequestType.RISK_UNIT_STOP);
        return new RiskTransferResult(RiskStatusCode.HTTP_OK, batchId);
    }

    @Inner(value = false)
    @PostMapping("getLog")
    public RiskLogResult<RiskLogVo> getLog(@RequestBody RiskLogDTO riskDataDto) {
        List<String> batchIds = riskDataDto.getBatchId();
        if (CollectionUtils.isEmpty(batchIds)) {
            return new RiskLogResult<>(RiskErrorType.INVALID_BATCH_ID);
        }
        List<RiskLogVo> riskLogVos = transferLogService.getLog(riskDataDto);
        return new RiskLogResult<>(RiskStatusCode.HTTP_OK, riskLogVos);
    }


    @Inner(value = false)
    @PostMapping("getErrorLog")
    public RiskLogResult<RiskErrorLogVo> getErrorLog(@RequestBody RiskErrorLogDTO riskErrorLogDTO) {
        List<String> errorIds = riskErrorLogDTO.getErrorIds();
        if (CollectionUtils.isEmpty(errorIds)) {
            return new RiskLogResult<>(RiskErrorType.INVALID_ERROR_ID);
        }
        List<RiskErrorLogVo> errorLogs = transferLogService.getErrorLog(riskErrorLogDTO);
        return new RiskLogResult<>(RiskStatusCode.HTTP_OK, errorLogs);
    }
}
